package com.lk.handler;

import com.lk.util.JsonUtil;
import com.lk.util.Result;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *  访问被拒绝处理程序  403
 * @author com.lk
 * @date 2021/11/23 15:46
 **/
@Component
public class CustomerAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Result<Object> of = Result.of(403, "访问拒绝", null);
        response.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write(JsonUtil.jsonObjToString(of));
    }
}
